package com.shsoft.utils;

/**
 * 十六进制与十进制转换的工具类
 */
public class HexUtil {

    /**
     * 10进制整数字符串转换成16进制字符串
     *
     */
    public static String intStrToHexStr(String intStr) {
        StringBuilder sb = new StringBuilder(8);
        String a;
        int n = Integer.parseInt(intStr);
        char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        while(n != 0){
            sb = sb.append(b[n%16]);
            n = n/16;
        }
        a = sb.reverse().toString();
        if(a.length() == 0){
            return "00";
        }else if (a.length() < 2){
            return "0" + a;
        }
        return a;
    }

    /**
     * 16进制字符串转10进制整数
     */
    public static int hexStr2int(String hexStr){
        return Integer.parseInt(hexStr,16);
    }
}
